// pages/mine/examine/examine.js
const db = wx.cloud.database()
const _ = db.command
const reportDB=db.collection('reportData')
const MessageDB=db.collection('messageData')
const publishDB=db.collection('publishData') 
const userinfoDB=db.collection('userInfo')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    reportdata:[],
    reportHidden:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    reportDB.orderBy('ReportDate', 'desc')
    .orderBy('publishID','asc').orderBy("_id","asc")
    .get({
      success:function(res){  
        console.log("查询成功",res)
        if(res.data.length==0){
          that.setData({
            reportdata:[],
            reportHidden:"暂时没有被举报的公告！"
          })
        }else{
        that.setData({
          reportdata:res.data,
          // reportHidden:true
        })
      }
        console.log(that.data.reportdata)
      },fail:function(res){  
        wx.showToast({
          title: '系统出错',
          icon:"none"
        })
      }
    })
  },
  clickNavDetail:function(e){
    console.log(e.currentTarget.id)
    let that = this
    publishDB.where({
      publishID:e.currentTarget.id
    }).get({
      success:function(res){
          console.log("公告数据：",res.data[0])
          let _item = res.data[0]
          userinfoDB.where({
            _openid:res.data[0].promulgatorID
          }).get({
            success:function(res){
              console.log("用户信息",res.data)
              _item['userinfo'] = res.data
              let item = JSON.stringify(_item)
              console.log(item)
              wx.navigateTo({
                url: '/pages/home/publishDetail/publishDetail?item='+item
              })
            },fail:function(res){
              console.log("查询出错")
            }
          })
      },fail:function(res){
        wx.showToast({
          title: '系统出错',
          icon:'none'
        })
      }
    })
  },
  deleteMessage:function(e){
    let that = this
    console.log(e.currentTarget.id)
    let publishid = e.currentTarget.id
    let deletepub = false
    let delPubsmessage = false
    let delPubReport = false
    wx.showActionSheet({
      itemList: ['删除'],
      success (res) {
        console.log(res.tapIndex)
        if(0 == res.tapIndex){
        wx.showModal({
          title: '',
          content: '该操作将删除该公告相关所有数据,你确定要删除吗？',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.showLoading({
                title: '删除中',
              })
              publishDB.where({
                publishID:publishid
              }).get().then(res => {
                console.log(res.data)
                // console.log(res.data[0].publishContent.img)
                let pubImgArray=[]
                if(res.data.length!=0){
                     pubImgArray =res.data[0].publishContent.img
                }
                wx.cloud.callFunction({
                name:'deleteMessage',
                data:{
                  publishid:publishid
                },success:res => {
                  console.log("删除留言",res)
                  delPubsmessage = true
                  if(delPubsmessage == true&&deletepub == true&&delPubReport==true){
                    that.delPublishSuccess()
                  }
                },
                fail:res=>{
                  wx.showToast({
                    title: '系统错误',
                    icon:"none"
                  })
                }
              })
            
              wx.cloud.callFunction({
                name:'deletePublish',
                data:{
                  publishid:publishid
                },success:res => {
                  console.log("删除公告",res)
                  wx.cloud.deleteFile({
                    fileList: pubImgArray
                  }).then(res => {
                    // handle success
                    console.log(res.fileList)
                    deletepub = true
                    if(delPubsmessage == true&&deletepub == true&&delPubReport==true){
                      that.delPublishSuccess()
                    }
                  }).catch(error => {
                    // handle error
                  })
                },
                fail:res=>{
                  wx.showToast({
                    title: '系统错误',
                    icon:"none"
                  })
                }
              })
              wx.cloud.callFunction({
                name:'deleteReport',
                data:{
                  publishid:publishid
                },success:res => {
                  console.log("删除举报",res)
                  delPubReport = true
                  if(delPubsmessage == true&&deletepub == true&&delPubReport==true){
                    that.delPublishSuccess()
                  }
                },
                fail:res=>{
                  wx.showToast({
                    title: '系统错误',
                    icon:"none"
                  })
                }
              })
              })  
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },fail (res) {
      console.log(res.errMsg)
    }
  })
  },
  delPublishSuccess:function(){
    let that = this
    wx.hideLoading();
    wx.showToast({
      title: '删除成功',
      icon:"none"
    })
    that.onLoad()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})